<?php

namespace Home\Model;

use Think\Model;

class FnwModel extends Model {
	
	/**
	 *
	 * @todo 判断权限
	 * @param number $school_id        	
	 * @param number $class_id        	
	 * @return string unknown
	 */
	public function checkClass($school_id = 0, $class_id = 0){
		if ($school_id == 0 || $class_id == 0) {
			return '';
		}
		$proving_where['school_id'] = $school_id;
		$proving_where['class_id'] = $class_id;
		$proving_where['status'] = 0;
		if (!M('tb_user_see_school_class')->field('ussc_id')->where($proving_where)->find()) {
			return '';
		}
		$school = M('tb_school')->where('school_id = ' . $school_id)->getField('school_name');
		$class = M('tb_class')->where('class_id = ' . $class_id)->getField('class_name');
		// 学校信息
		$menu_info = '' . $school . ' > ' . $class . '';
		return $menu_info;
	}
	
	/**
	 *
	 * @todo 联系人添加
	 */
	public function addLinkUsers($input){
		$return = array(
			'status' => 0,
			'msg' => '',
		);
		// 添加数组
		$add_arr['school_id'] = $input['school_id'];
		$add_arr['class_id'] = $input['class_id'];
		$add_arr['create_by'] = session('uid');
		$add_arr['status'] = 0;
		// 条件
		$array['phone'] = $input['phone'];
		$array['status'] = 0;
		//主用户表
		$main_user = M('tb_user')->field('user_id')->where($array)->find();
		if($main_user){
			$return['msg'] = '该手机号已注册';
			return $return;
		}
		//联系人表
		$array['create_by'] = session('uid');
		$user = M('tb_user_temp')->field('user_temp_id')->where($array)->find();
		if ($user) {
			$insert_id = $user['user_temp_id'];
			$add_arr['user_temp_id'] = (int)$user['user_temp_id'];
			// 联系人关系是否存在
			$link = M('tb_user_temp_in_school_class')->field('utisc_id')->where($add_arr)->find();
			if ($link) {
				$return['msg'] = '添加失败';
				return $return;
			}
			$add_arr['create_date'] = NOW_TIME;
		} else {
			//是否注册
			$where_reg['phone'] = $input['phone'];
			$reg = M('tb_user')->field('user_id')->where($where_reg)->find();
			$array['activate'] = ($reg)? 1 : 0 ;
			//end
			$array['real_name'] = $input['real_name'];
			$insert_id = M('tb_user_temp')->add($array);
			if ($insert_id > 0) {
				$add_arr['user_temp_id'] = $insert_id;
				$add_arr['create_date'] = NOW_TIME;
			} else {
				$return['msg'] = '添加失败';
				return $return;
			}
		}
		if (false !== M('tb_user_temp_in_school_class')->add($add_arr)) {
			$return['status'] = 1;
			$return['msg'] = '添加成功';
			$return['url'] = '/Link/linkUser?school_id=' . $input['school_id'] . '&class_id=' . $input['class_id'];
		}
		return $return;
	}
	
	/**
	 * @todo	联系人编辑
	 */
	public function editLinkUsers($input){
		$return = array(
			'status' => 0,
			'msg' => '',
		);
		//修改条件
		$where['user_temp_id'] = (int)$input['user_temp_id'];
		$where['create_by'] = (int)$input['current_uid'];
		//修改数据
		$array['phone'] = $input['phone'];
		//主用户表
		$main_user = M('tb_user')->field('user_id')->where($array)->find();
		if($main_user){
			$return['msg'] = '该手机号已注册';
			return $return;
		}
		$array['real_name'] = $input['real_name'];
		//判断
		$where_user['tb_user_temp.user_temp_id'] = array('neq',(int)$input['user_temp_id']);
		$where_user['tb_user_temp.phone'] = $input['phone'];
// 		$where_user['tb_user_temp.real_name'] = $input['real_name'];	//用户绑定
		$where_user['tb_user_temp_in_school_class.school_id'] = (int)$input['school_id'];
		$where_user['tb_user_temp_in_school_class.class_id'] = (int)$input['class_id'];
		$where_user['tb_user_temp_in_school_class.status'] = 0;
		$join = 'left join tb_user_temp_in_school_class on tb_user_temp.user_temp_id = tb_user_temp_in_school_class.user_temp_id';
		$user = M('tb_user_temp')->field('tb_user_temp.user_temp_id')->join($join)->where($where_user)->find();
		if($user){
			$return['msg'] = '该手机号已存在';
			return $return;
		}
		//是否注册
		$where_reg['phone'] = $input['phone'];
		$reg = M('tb_user')->field('user_id')->where($where_reg)->find();
		$array['activate'] = ($reg)? 1 : 0 ;
		if(false !== M('tb_user_temp')->where($where)->save($array)){
			$return['status'] = '1';
			$return['msg'] = '修改成功';
			$return['url'] = '/Link/linkUser?school_id=' . $input['school_id'] . '&class_id=' . $input['class_id'];
		}else{
			$return['msg'] = '修改失败';
		}
		return $return;
	}
}

?>